package com.jeesite.modules.app.user.systeminfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.app.util.wx.NioWebSocketHandler;
import com.jeesite.modules.app.util.wx.SocketMessage;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.sys.msg.push.entity.ZxMsgPush;
import com.jeesite.modules.zx.sys.msg.push.service.ZxMsgPushService;

@Controller
@RequestMapping(value = "/app/user/systeminfo/user/")
public class AppSystemInfoController {
            private static Logger logger=LoggerFactory.getLogger(AppSystemInfoController.class);
            @Autowired
           	private ZxMsgPushService zxMsgPushService;
            private String token="QWIENDKSA123456";
            private String aesKey="qwertyuiopASDFGHJKLZXCVBNM9876540zxASDFGHJ7";
            @RequestMapping(value="findPage")
           	public void findPage(ZxMsgPush zxMsgPush,HttpServletRequest request,HttpServletResponse response){
            	String userId=request.getParameter("userId");
            	if(StringUtils.isBlank(userId)){
            		AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
            		return;
            	}
            	zxMsgPush.setReceiveCode(userId);
            	zxMsgPush.setPage(new Page<>(request, response));
            	Page<ZxMsgPush> findPage = zxMsgPushService.findPage(zxMsgPush);
            	JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(findPage));
            	AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
            }
            @RequestMapping(value="send")
           	public JSONObject send(ZxMsgPush zxMsgPush,HttpServletRequest request,HttpServletResponse response){
            	JSONObject json=new JSONObject();
            	json.put("errcode", 0);
            	json.put("errmsg", "ok");

            	 return json;
            }
            @RequestMapping(value="message")
           	public JSONObject message(ZxMsgPush zxMsgPush,HttpServletRequest request,HttpServletResponse response){
            	JSONObject json=new JSONObject();
            	json.put("token", token);
            	json.put("message", "ok");

            	 return json;
            }
            @RequestMapping(value="token")
           	public JSONObject token(ZxMsgPush zxMsgPush,HttpServletRequest request,HttpServletResponse response){
            	 
//            	{"access_token":"ACCESS_TOKEN","expires_in":7200}
            	JSONObject json=new JSONObject();
            	json.put("access_token", token);
            	json.put("expires_in", 7200);
                json.put("errcode", 0);
            	 return json;
            }
            @RequestMapping(value="delete")
            public void delete(ZxMsgPush zxMsgPush,HttpServletRequest request,HttpServletResponse response){
            	String id=zxMsgPush.getId();
            	if(StringUtils.isBlank(id)){
            		AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "标识");
            		return;
            	}
            	zxMsgPushService.delete(zxMsgPush);
            	try {
            	 String sendUserCode=request.getParameter("userId");
       			 ZxMsgPush entity=new ZxMsgPush();
       			 entity.setReceiveCode(sendUserCode);
       			 entity.setReadStatus("0");
       			long findCount = zxMsgPushService.findCount(entity);
       			SocketMessage sm=new SocketMessage();
       			sm.setType("systeminfo");
       			sm.setNum(findCount);
       			sm.setUserId(sendUserCode);
       			NioWebSocketHandler.sendMessage(JsonMapper.toJson(sm));
       		} catch (Exception e) {
       			// TODO Auto-generated catch block
       			e.printStackTrace();
       		}
            	AppController.renderSuccessObject(response, "",MessageCode.SUCCESS_ACK);
            }
            @RequestMapping(value="update")
            public void update(ZxMsgPush zxMsgPush,HttpServletRequest request,HttpServletResponse response){
            	String id=zxMsgPush.getId();
            	if(StringUtils.isBlank(id)){
            		AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "标识");
            		return;
            	}
            	zxMsgPush.setReadStatus("1");
            	zxMsgPushService.update(zxMsgPush);
            	try {
		           	 String sendUserCode=request.getParameter("userId");
		  			 ZxMsgPush entity=new ZxMsgPush();
		  			 entity.setReceiveCode(sendUserCode);
		  			 entity.setReadStatus("0");
          			long findCount = zxMsgPushService.findCount(entity);
          			SocketMessage sm=new SocketMessage();
          			sm.setType("systeminfo");
          			sm.setNum(findCount);
          			sm.setUserId(sendUserCode);
          			NioWebSocketHandler.sendMessage(JsonMapper.toJson(sm));
          		} catch (Exception e) {
          			// TODO Auto-generated catch block
          			e.printStackTrace();
          		}
            	AppController.renderSuccessObject(response, "",MessageCode.SUCCESS_ACK);
            }
}
